package com.cow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cow.entity.Product;
import com.cow.service.ProductService;
import com.cow.util.general.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    @GetMapping("/findAllSale")
    public CommonResult findAllSale(){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("is_sale",1);
        List<Product> productList = productService.list(wrapper);
        return CommonResult.success("商品查询成功",productList);
    }

    @GetMapping("/findAll")
    public CommonResult findAll(){
        List<Product> productList = productService.list();
        return CommonResult.success("商品查询成功",productList);
    }



    @GetMapping("/findAllWithConditions")
    public CommonResult findAllWithConditions(String by,String keyWord){
        List<Product> productList = productService.findAllWithConditions(by,keyWord);
        return CommonResult.success("商品查询成功",productList);
    }

    @GetMapping("/findById")
    public CommonResult findById(@RequestParam(required = true) Integer productId){
        Product product = productService.getById(productId);
        return CommonResult.success("商品详情查询成功",product);
    }

    @PutMapping("/update")
    public CommonResult update(Product product){
        productService.updateById(product);
        return CommonResult.success("商品修改成功",product);
    }
}
